package work.lrl.coupon_inventory;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import java.util.Date;

public interface Setting {
        String date = DateUtil.format(new Date(),"yyyy-MM-dd");
//    String date = "2023-12-29";
    String templateId = "biZWvAjenkmdkB8YBegrW";
    String dir = "C:\\work-records\\l陆叶\\l-lrl卡券模板库存修改\\" + date + "\\";
    String dataPath = dir + "data.xlsx";

    String outPath = dir + "result.js";
    String codeJsonPath = dir + "code.json";

    String mongoQuery = """
            
            db.lrl_coupon_template_inventory.updateOne({_id:"%s"},
            {$set:{
            	actual:%s,
            	usable:%s,
            	used:%s,
            	redeem:%s,
            	templateId:"%s",
            	counterId:"%s",
            	counterCode:"%s",
            }},{ upsert: true })
            
            """;


    /**
     * 库存对象
     */
    @Data
    class InventoryExcelObject {
        @ExcelProperty("门店名称")
        private String name;

        @ExcelProperty("门店code")
        private String code;

        @ExcelProperty("实际库存")
        private String actual;

        @ExcelProperty("可用库存")
        private String usable;

        @ExcelProperty("已用库存")
        private String used;

        @ExcelProperty("核销数量")
        private String redeem;

        public String getQuery() {
            String id = templateId + "_" + code;
            return String.format(mongoQuery, id, actual, actual, 0, 0, templateId, code, code);
        }
    }

    @RequiredArgsConstructor
    public static class InventoryExcelReadListener implements ReadListener<InventoryExcelObject> {

        private final ExcelInvoke excelInvoke;
        private final ExcelDoAfterAllAnalysed afterAllAnalysed;

        @Override
        public void invoke(InventoryExcelObject inventoryExcelObject, AnalysisContext analysisContext) {
            excelInvoke.invoke(inventoryExcelObject, analysisContext);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            afterAllAnalysed.doAfterAllAnalysed(analysisContext);
        }
    }

    interface ExcelInvoke {
        void invoke(InventoryExcelObject inventoryExcelObject, AnalysisContext analysisContext);
    }

    interface ExcelDoAfterAllAnalysed {
        void doAfterAllAnalysed(AnalysisContext analysisContext);
    }
}
